/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "mdown.h"
#include "alert.h"

#include <QStringList>

QString Mdown::toZcode(const QString& text)
{
    QStringList args;
    args << "-f" << "zcode";

    return toFormat(text, args);
}

QString Mdown::toLatex(const QString& text)
{
    QStringList args;
    args << "-f" << "latex";

    return toFormat(text, args);
}

QString Mdown::toHtml(const QString& text)
{
    QStringList args;
    args << "-f"<< "xhtml";

    return toFormat(text, args);
}

QString Mdown::toFormat(const QString& text, QStringList& args)
{
    QProcess mdown;
    mdown.start("mdown", args);

    try{
		if(!mdown.waitForStarted())
			throw 200;
                mdown.write(text.toUtf8());
		mdown.closeWriteChannel();

		if(!mdown.waitForFinished())
			throw 201;

                return QString::fromUtf8( mdown.readAll() );
    }
    catch(int e)
    {
		VTOAlert::alert(e);
		
		return QString();
    }
}
